package com.sunfeng.elasticsearch.document.search;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * 查询
 *
 * @author sunfeng
 * @date 2021/04/22 14:43
 **/
public class ESTest_Doc_Query2 {
    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //1、查询所有

        SearchRequest request = new SearchRequest();
        request.indices("user");

//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        request.source(searchSourceBuilder);


        // 2、条件查询  查询 年龄为30的用户

//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30));
//        request.source(sourceBuilder);


        // 3、分页查询

//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.from(0);
//        searchSourceBuilder.size(2);
//        request.source(searchSourceBuilder);

        // 4、查询排序
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.sort("age", SortOrder.DESC);
//        request.source(searchSourceBuilder);

        // 5、过滤字段
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        String[] includes={};
//        String[] excludes={"age"};
//        builder.fetchSource(includes,excludes);
//        request.source(builder);
        // 6、组合查询

//        SearchSourceBuilder builder = new SearchSourceBuilder();
//
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//
//        boolQueryBuilder.must(QueryBuilders.termQuery("age","30"));
//        boolQueryBuilder.must(QueryBuilders.termQuery("sex","男"));
//        builder.query(boolQueryBuilder);
//
//        request.source(builder);


        // 7、范围查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//        rangeQuery.gte(30);
//        rangeQuery.lte(40);
//        builder.query(rangeQuery);
//
//        request.source(builder);

        // 8、模糊查询
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("name", "wangwu");
//        fuzzyQuery.fuzziness(Fuzziness.ONE);
//        builder.query(fuzzyQuery);
//        request.source(builder);

        // 9、高亮查询
        //  记住 高亮查询需要两个部分    query 和 highlight  highlight分为:{fields pre_tags post_tags}

//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "wangwu");
//
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//
//        highlightBuilder.preTags("<font color='red'>");
//        highlightBuilder.postTags("</font>");
//        highlightBuilder.field("name");
//
//        builder.query(termQuery);
//        builder.highlighter(highlightBuilder);
        // 10、聚合查询
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        sourceBuilder.aggregation(aggregationBuilder);
        request.source(sourceBuilder);
        // 11、分组查询

//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.aggregation(AggregationBuilders.terms("age_groupby").field("age"));
//        request.source(sourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
        client.close();
    }
}
